package com.yisu.expressway.ui.recyclerview;

import android.app.Activity;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.view.View;

import com.yisu.expressway.R;
import com.yisu.expressway.utils.DisplayUtil;


/**
 * Created by theodore_hu on 2016/2/2.
 */
public class DividerGridItemDecorationBottom extends RecyclerView.ItemDecoration
{

    private static final int[] ATTRS = new int[] { android.R.attr.listDivider };
    private Drawable mDivider;
    private int mDividerSize = 0;

    private Paint mPaint ;
    public DividerGridItemDecorationBottom(Activity context)
    {
        mPaint = new Paint(Paint.ANTI_ALIAS_FLAG) ;
        mPaint.setColor(context.getResources().getColor(R.color.module_divider));
        mPaint.setStyle(Paint.Style.FILL);
        mDividerSize = 1;
    }

    /**
     *
     *
     * @param DividerWidth is measured by dp
     */
    public DividerGridItemDecorationBottom(Activity context, float DividerWidth)
    {
        mPaint = new Paint(Paint.ANTI_ALIAS_FLAG) ;
        mPaint.setColor(Color.TRANSPARENT);
        mPaint.setStyle(Paint.Style.FILL);
        mDividerSize = DisplayUtil.dip2px(context,DividerWidth);
    }

    public DividerGridItemDecorationBottom(Activity context, float DividerWidth, int color)
    {
        mPaint = new Paint(Paint.ANTI_ALIAS_FLAG) ;
        mPaint.setColor(color);
        mPaint.setStyle(Paint.Style.FILL);
        mDividerSize = DisplayUtil.dip2px(context,DividerWidth);
    }

    @Override
    public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state)
    {

        drawHorizontal(c, parent);
        drawVertical(c, parent);

    }

    private int getSpanCount(RecyclerView parent)
    {
        int spanCount = -1;
        RecyclerView.LayoutManager layoutManager = parent.getLayoutManager();
        if (layoutManager instanceof GridLayoutManager)
        {
            spanCount = ((GridLayoutManager) layoutManager).getSpanCount();
        } else if (layoutManager instanceof StaggeredGridLayoutManager)
        {
            spanCount = ((StaggeredGridLayoutManager) layoutManager)
                    .getSpanCount();
        }
        return spanCount;
    }

    public void drawHorizontal(Canvas c, RecyclerView parent)
    {
        int childCount = parent.getChildCount();
        for (int i = 0; i < childCount; i++)
        {
            final View child = parent.getChildAt(i);
            final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child
                    .getLayoutParams();
            final int left = child.getLeft() - params.leftMargin;
            final int right = child.getRight() + params.rightMargin
                    + mDividerSize;//mDivider.getIntrinsicWidth();
            final int top = child.getBottom() + params.bottomMargin;
            final int bottom = top + mDividerSize;//mDivider.getIntrinsicHeight();
            c.drawRect(left,top,right,bottom,mPaint);
//            mDivider.setBounds(left, top, right, bottom);
//            mDivider.draw(c);
        }
    }

    public void drawVertical(Canvas c, RecyclerView parent)
    {
        final int childCount = parent.getChildCount();
        for (int i = 0; i < childCount; i++)
        {
            final View child = parent.getChildAt(i);

            final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child
                    .getLayoutParams();
            final int top = child.getTop() - params.topMargin;
            final int bottom = child.getBottom() + params.bottomMargin;
            final int left = child.getRight() + params.rightMargin;
            final int right = left + mDividerSize;//mDivider.getIntrinsicWidth();
            c.drawRect(left,top,right,bottom,mPaint);
//            mDivider.setBounds(left, top, right, bottom);
//            mDivider.draw(c);
        }
    }

    private boolean isLastColumForHorizontal(RecyclerView parent, int pos,
                                             int childCount)
    {
        RecyclerView.LayoutManager layoutManager = parent.getLayoutManager();
        if (layoutManager instanceof GridLayoutManager)
        {
            if ((pos + 1) == childCount || pos + 2 == childCount) {
                return true;
            }
        }
        return false;
    }

    private boolean isLastRaw(RecyclerView parent, int pos, int spanCount,
                              int childCount)
    {
        RecyclerView.LayoutManager layoutManager = parent.getLayoutManager();
        if (layoutManager instanceof GridLayoutManager)
        {
            childCount = childCount - childCount % spanCount;
            if (pos >= childCount)
                return true;
        } else if (layoutManager instanceof StaggeredGridLayoutManager)
        {
            int orientation = ((StaggeredGridLayoutManager) layoutManager)
                    .getOrientation();
            if (orientation == StaggeredGridLayoutManager.VERTICAL)
            {
                childCount = childCount - childCount % spanCount;
                if (pos >= childCount)
                    return true;
            } else
            {
                if ((pos + 1) % spanCount == 0)
                {
                    return true;
                }
            }
        }
        return false;
    }

    @Override
    public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {

        int spanCount = getSpanCount(parent);
        int SpanIndex = 0;
        int orientation = GridLayoutManager.VERTICAL;
        RecyclerView.LayoutManager layoutManager = parent.getLayoutManager();
        if(layoutManager instanceof GridLayoutManager){
            orientation = ((GridLayoutManager) layoutManager).getOrientation();
        }

        if (view.getLayoutParams() instanceof GridLayoutManager.LayoutParams){
            SpanIndex = ((GridLayoutManager.LayoutParams) view.getLayoutParams()).getSpanIndex();
        }else if(view.getLayoutParams() instanceof StaggeredGridLayoutManager.LayoutParams){
            SpanIndex = ((StaggeredGridLayoutManager.LayoutParams) view.getLayoutParams()).getSpanIndex();
        }

        if(orientation == GridLayoutManager.HORIZONTAL){
            if(isLastColumForHorizontal(parent,parent.getChildAdapterPosition(view),parent.getAdapter().getItemCount())) {
                outRect.set(0, 0, 0, mDividerSize);
            }else{
                outRect.set(0, 0, mDividerSize, mDividerSize);
            }
        }else {
                outRect.set(0, 0, 0, mDividerSize);
        }
    }
}
